﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.DataLayer;
using BaHuan.Entities;


namespace BaHuan
{
    public partial class FrmPhieuThanhToanTienHang : Form
    {
        private ChiNhanhCongTyMgt chinhanhcongtyMgt=new ChiNhanhCongTyMgt();
        private KhachHangManagement khachhangMgt=new KhachHangManagement();
        private ChiNhanhKhachHangManagement chinhanhkhachhangMgt=new ChiNhanhKhachHangManagement();
        private PhieuXuatHangManagement phieuxuathangMgt = new PhieuXuatHangManagement();
        private CTPhieuXuatHangManagement ctphieuxuathangMgt = new CTPhieuXuatHangManagement();
        private CTPhieuHangTraManagement ctphieuhangtraMgt = new CTPhieuHangTraManagement();
        private PhieuHangTraManagement phieuhangtraMgt = new PhieuHangTraManagement();
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private PhieuThuTienManagement phieuthutienMgt = new PhieuThuTienManagement();
        private CTTKKeToanLuongTinhManagement cttkketoanluongtinhMgt = new CTTKKeToanLuongTinhManagement();

        public FrmPhieuThanhToanTienHang()
        {
            InitializeComponent();
        }

        private void btnHuybo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void BinhdingtoCbo()
        {
            cboNoiXuat.DataSource = GetAllChiNhanhCongTy();
            cboNoiXuat.DisplayMember = "TenChiNhanh";
            cboNoiXuat.ValueMember = "Id";

            cboKhachHang.DataSource = GetAllKhachHang();
            cboKhachHang.DisplayMember = "Ten";
            cboKhachHang.ValueMember = "Id";
        }

        public bool IsInsertKiemSoatLienba(PhieuXuatHang phieuxuathang)
        {
            ResultObj resultObj = new ResultObj();
            resultObj = ctphieuxuathangMgt.GetAllCTPhieuXuatHang();
            List<CTPhieuXuatHang> listctphieuxuathang = (from ks in (List<CTPhieuXuatHang>)resultObj.Result
                                                     where ks.IdPhieuXuatHang == phieuxuathang.Id
                                                     select ks).ToList();
            if (listctphieuxuathang.Count > 0)
                return true;
            return false;
        }

        private void FrmPhieuThanhToanTienHang_Load(object sender, EventArgs e)
        {
            BinhdingtoCbo();
        }

        private void cboKhachHang_SelectedValueChanged(object sender, EventArgs e)
        {
            cboChiNhanh.DataSource = null;
            if (cboKhachHang.SelectedItem != null)
            {
                KhachHang khachhang = (KhachHang)cboKhachHang.SelectedItem;

                List<ChiNhanhKhachHang> lstChiNhanhKhachHang = (from b in GetAllChiNhanhKhachHang()
                                                                where b.IdKhachHang == khachhang.Id
                                                                select b).ToList();

                if (lstChiNhanhKhachHang.Count > 0)
                {
                    cboChiNhanh.DataSource = lstChiNhanhKhachHang;
                    cboChiNhanh.DisplayMember = "Ten";
                    cboChiNhanh.ValueMember = "Id";
                }
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            DateTime dtNgay = dtpChonThoiGian.Value;
            List<CTPhieuXuatHang> lstCTPXH = new List<CTPhieuXuatHang>();
            List<CTPhieuHangTra> lstCTPHT = new List<CTPhieuHangTra>();

            ChiNhanhCongTy chinhanhcty = new ChiNhanhCongTy();
            if (cboNoiXuat.Text != "")
                chinhanhcty = (ChiNhanhCongTy)cboNoiXuat.SelectedItem;
            else
            {
                MessageBox.Show("Vui lòng chọn nơi xuất");
                return;
            }

            KhachHang khachhang = new KhachHang();
            if (cboKhachHang.Text != "")
                khachhang = (KhachHang)cboKhachHang.SelectedItem;
            else
            {
                MessageBox.Show("Vui lòng chọn khách hàng");
                return;
            }

            ChiNhanhKhachHang chinhanhkhachhang = new ChiNhanhKhachHang();
            if (cboChiNhanh.SelectedItem != null)
                chinhanhkhachhang = (ChiNhanhKhachHang)cboChiNhanh.SelectedItem;

            long lSoDu = 0;
            long lThue = 0;
            long lThanhToan = 0;
            long lChietKhau = 0;
            long lHangBan = 0;
            long lHangTra = 0;
            string sTenKH = cboKhachHang.Text;
            string sMSKH = "";

            // Get SoDuBanDau of customer
            var tk = (from a in GetAllCTTKKeToanLuongTinh()
                      where a.TaiKhoan == "131" && a.DoiTuong == khachhang.MaSoKhachHang
                      select a).FirstOrDefault();

            if (tk != null)
                lSoDu = tk.SoDuBanDau;

            var lst = (from c in GetAllButToan()
                       join p in GetAllKhachHang() on c.MaSoKhachHang equals p.Ten
                       join cn in GetAllChiNhanhKhachHang() on p.Id equals cn.IdKhachHang into pcn
                       from x in pcn.DefaultIfEmpty()
                       where c.NgayThang.Date <= dtNgay.Date
                       select new { bt1 = c, p1 = p, br1 = x });

            // Get all ButToans of account 131 with datetime lower than applied datetime
            var lst131 = lst.Where(p => p.bt1.NgayThang.Date < dtNgay.Date && p.bt1.TKGoc == "131" && p.p1.Id == khachhang.Id);

            if (chinhanhcty.Id != 0)
            {
                lst131 = lst131.Where(c => c.br1.Id == chinhanhcty.Id);
            }
            else
                MessageBox.Show("Bạn phải chọn chi nhánh khách hàng");

            // Calculating SoDu
            if (lst131.ToList().Count > 0)
            {
                foreach (var bt in lst131)
                {
                    lSoDu = (lSoDu + bt.bt1.GhiNo) - bt.bt1.GhiCo;
                }
            }

            // Calculating ThanhToanTrongKy
            var lstThanhToan = lst.Where(p => p.bt1.NgayThang.Date == dtNgay && p.p1.Id == khachhang.Id
                                                && (p.bt1.TKGoc.StartsWith("111") || p.bt1.TKGoc.StartsWith("112")));

            if (chinhanhcty.Id != 0)
            {
                lstThanhToan = lstThanhToan.Where(p => p.br1.Id == chinhanhcty.Id);
            }

            if (lstThanhToan.ToList().Count > 0)
            {
                foreach (var bt in lstThanhToan)
                {
                    lThanhToan += bt.bt1.GhiCo;
                }
            }

            // List of XuatBan
            var lstHangBan = (from p in GetAllPhieuXuatHang()
                              where p.KhachHang == khachhang.Ten && p.NgayThang.Date == dtNgay.Date
                              select p);

            if (chinhanhcty.Id != 0)
            {
                lstHangBan = lstHangBan.Where(p => p.ChiNhanhKhachHang == chinhanhcty.TenChiNhanh);
            }

            if (lstHangBan.ToList().Count > 0)
            {
                foreach (var p in lstHangBan)
                {
                    var lstPT = (from t in GetAllPhieuThuTien()
                                 where t.IdPhieuXuatHang == p.Id
                                 select t);

                    long lTongCong = lstPT.Sum(t => t.SoTien);
                    if (lTongCong < p.TongCong)
                    {
                        List<CTPhieuXuatHang> lstCT = (from d in GetAllCTPhieuXuatHang()
                                                       where d.IdPhieuXuatHang == p.Id
                                                       select d).ToList();
                        foreach (CTPhieuXuatHang ct in lstCT)
                        {
                            lstCTPXH.Add(ct);
                            lHangBan += ct.ThanhTien;
                        }

                        lThue += p.TienVAT;
                        lChietKhau += p.TienChietKhau;
                    }
                }
            }

            // List HangTra
            var lstHangTra = (from p in GetAllPhieuHangTra()
                              where p.KhachHang == khachhang.Ten && p.NgayThang.Date == dtNgay.Date
                              select p);

            if (chinhanhcty.Id != 0)
            {
                lstHangTra = lstHangTra.Where(p => p.ChiNhanhKhachHang == chinhanhcty.TenChiNhanh);
            }

            if (lstHangTra.ToList().Count > 0)
            {
                foreach (var p in lstHangTra)
                {
                    List<CTPhieuHangTra> lstCT = (from d in GetAllCTPhieuHangTra()
                                                  where d.IdPhieuHangTra == p.Id
                                                  select d).ToList();

                    foreach (CTPhieuHangTra ct in lstCT)
                    {
                        lstCTPHT.Add(ct);
                        lHangTra += ct.ThanhTien;
                    }
                }
            }

            string sTienChu = CNum2Char.So2Chu(lSoDu + (lHangBan - lHangTra) + lThue - lThanhToan - lChietKhau);

            FrmXemReport frmXemReport = new FrmXemReport(lstCTPXH.ToList(), lstCTPHT.ToList(), lSoDu, lThue, lThanhToan, lChietKhau, lHangBan, lHangTra, dtNgay, sTienChu, sTenKH, sMSKH);
            this.Hide();
            frmXemReport.ShowDialog();
            this.Show();
        }

        private void cboNoiXuat_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy b in cboNoiXuat.Items)
            {
                if (b.TenChiNhanh.ToLower().Contains(cboNoiXuat.Text) && cboNoiXuat.Text != "")
                {
                    cboNoiXuat.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboNoiXuat.SelectedValue == null)
            {
                cboNoiXuat.Text = "";
                cboNoiXuat.Focus();
            }
        }

        private void cboKhachHang_Leave(object sender, EventArgs e)
        {
            foreach (KhachHang b in cboKhachHang.Items)
            {
                if (b.Ten.ToLower().Contains(cboKhachHang.Text) && cboKhachHang.Text != "")
                {
                    cboKhachHang.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboKhachHang.SelectedValue == null)
            {
                cboKhachHang.Text = "";
                cboKhachHang.Focus();
            }
        }

        private void cboChiNhanh_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhKhachHang b in cboChiNhanh.Items)
            {
                if (b.Ten.ToLower().Contains(cboChiNhanh.Text) && cboChiNhanh.Text != "")
                {
                    cboChiNhanh.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboChiNhanh.SelectedValue == null)
            {
                cboChiNhanh.Text = "";
            }
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> lstChinhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;

            return lstChinhanhCongTy;
        }

        public List<PhieuXuatHang> GetAllPhieuXuatHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieuxuathangMgt.GetAllPhieuXuatHang();
            List<PhieuXuatHang> lstPhieuXuatHang = (List<PhieuXuatHang>)resultObj.Result;

            return lstPhieuXuatHang;
        }

        public List<PhieuHangTra> GetAllPhieuHangTra()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieuhangtraMgt.GetAllPhieuHangTra();
            List<PhieuHangTra> lstPhieuHangTra = (List<PhieuHangTra>)resultObj.Result;

            return lstPhieuHangTra;
        }

        public List<CTPhieuXuatHang> GetAllCTPhieuXuatHang()
        {
            ResultObj obj = new ResultObj();

            obj = ctphieuxuathangMgt.GetAllCTPhieuXuatHang();
            List<CTPhieuXuatHang> lstCTPhieuXuatHang = (List<CTPhieuXuatHang>)obj.Result;

            return lstCTPhieuXuatHang;
        }

        public List<CTPhieuHangTra> GetAllCTPhieuHangTra()
        {
            ResultObj obj = new ResultObj();

            obj = ctphieuhangtraMgt.GetAllCTPhieuHangTra();
            List<CTPhieuHangTra> lstCTPhieuHangTra = (List<CTPhieuHangTra>)obj.Result;

            return lstCTPhieuHangTra;
        }

        public List<KhachHang> GetAllKhachHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = khachhangMgt.GetAllKhachHang();
            List<KhachHang> lstKhachHang = (List<KhachHang>)resultObj.Result;

            return lstKhachHang;
        }

        public List<ChiNhanhKhachHang> GetAllChiNhanhKhachHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chinhanhkhachhangMgt.GetAllChiNhanhKhachHang();
            List<ChiNhanhKhachHang> lstchinhanhkhachhang = (List<ChiNhanhKhachHang>)resultObj.Result;

            return lstchinhanhkhachhang;
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }

        public List<PhieuThuTien> GetAllPhieuThuTien()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieuthutienMgt.GetAllPhieuThuTien();
            List<PhieuThuTien> lstPhieuThuTien = (List<PhieuThuTien>)resultObj.Result;

            return lstPhieuThuTien;
        }

        public List<CTTKKeToanLuongTinh> GetAllCTTKKeToanLuongTinh()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = cttkketoanluongtinhMgt.GetAllCTTKKeToanLuongTinh();
            List<CTTKKeToanLuongTinh> listCTTKKeToanLuongTinh = (List<CTTKKeToanLuongTinh>)resultObj.Result;

            return listCTTKKeToanLuongTinh;
        }

    }
}
